package com.gxa.springboot229.controller;

import com.gxa.springboot229.excption.BusinessException;
import com.gxa.springboot229.pojo.User;
import com.gxa.springboot229.service.UserService;
import com.gxa.springboot229.util.JWTUtil;
import com.gxa.springboot229.util.JsonResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    //java 要操作redis,是借助RedisTemplate
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    private UserService userService;

    /**
     * Vip注册方法
     * @param name
     * @param phone
     * @param captcha
     * @return
     */
    @RequestMapping("/reg")
    public Map reg(@RequestParam("name")String name,@RequestParam("phone") String phone,@RequestParam("captcha")String captcha){
        JsonResult jsonResult = new JsonResult();
        if(userService.findByPhone(phone) == 1){
            throw  new BusinessException("您以注册，请直接登录");
        }
        //验证码是否合法
        ValueOperations<String,Object> operations = redisTemplate.opsForValue();
        Object captchaIn = operations.get("sms:"+phone);
        if(captcha .equals(String.valueOf(captchaIn))){
            User user = new User();
            user.setPhone(phone);
            user.setName(name);
            user.setRegDate(new Date());
            Integer reg = userService.reg(user);
            if(reg == 1){
                jsonResult.setCode("1");
            }
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("验证失败，请重新发送验证码");
        }

        //合法，将个人信息，注册时间，添加到数据库
        //不合法，返回注册失败：验证码不正确
        return jsonResult.getValues();
    }

    /**
     * 用户登录接口
     * @param phone
     * @return
     */
    @RequestMapping("/LogUser")
    public Map reg(@RequestParam("phone")String phone){
        String token = null;
        JWTUtil jwtUtil = new JWTUtil();
        Map<String,Object> objectMap =new HashMap<>();
        JsonResult jsonResult = new JsonResult();
        if("".equals(phone)){
            throw new BusinessException("手机不能为空");
        }
        Integer byPhone = userService.findByPhone(phone);
        if(byPhone == 0){
            throw new BusinessException("登录失败");
        }
        Map login = userService.findLogin(phone);
        token = jwtUtil.createJWT(phone, (String)login.get("name"), 30 * 1000 * 60 * 6 * 24, objectMap);
        jsonResult.setCode("1");
        jsonResult.setMsg("登录成功");
        jsonResult.put("token",token);
        jsonResult.setData(login);
        return jsonResult.getValues();
    }

    /**
     * 用户修改资料接口
     * @param map
     * @return
     */
    @RequestMapping("/updateUser")
    public Map updateUser(@RequestParam("id")int id,@RequestParam("name")String name){
        User user = new User();
        if("".equals(id)){
            throw new BusinessException("id信息不能为空");
        }
        if(name == null || "".equals(name)){
            throw new BusinessException("修改信息不能为空");
        }
        user.setId(id);
        user.setName(name);
        Map map1 = userService.updateUser(user);
        return map1;
    }
}
